<%-- 
    Document   : layout
    Created on : Dec 8, 2011, 11:04:13 PM
    Author     : Ken
--%>

<%@page import="util.AccessMap"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="model.SystemUser"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>IEPM Admin</title>
        <%@include file = "global/admin_include.jsp" %>
        <%
            HashMap am =new AccessMap().hm;
            Object objMap = request.getSession().getAttribute("accessMap");
            SystemUser su = new SystemUser();
            Object obj = request.getSession().getAttribute("sysUser");
            if ((obj == null)) {         
                request.getSession().setAttribute("accessMap",am);
                response.sendRedirect("../admin.jsp");
            } else {
                su = (SystemUser) obj;
                am = (HashMap)objMap;
            }
        %>
        <script type="text/javascript">
            
            $(document).ready(function(){
                $('#changePwd-dialog').dialog({modal:true});
                $('#changePwd-dialog').dialog('close');
                $('#emp_name_menu').text('<%= su.getName()%>');
                $('#resetPwd-dialog').dialog({modal:true});
                $('#resetPwd-dialog').dialog('close');
            });
            
            function onClickCancel(formId,content){
                $('#changePwd-dialog').dialog('close');
            }
            
            function changePwd(){
                $('#changePwd-dialog').dialog('open');
                $('#old_password').val("");
                $('#new_password').val("");
                $('#confirm_new_password').val("");
                $('#old_password_label').html("");
                $('#confirm_new_password_label').html("");
                $('#new_password_label').html("");

            }
            
            function checkConfirmPwd(){
                var confirmPwd = $('#confirm_new_password').val();
                var newPwd = $('#new_password').val();
                
                if(confirmPwd ==""||newPwd==""){
                    $('#confirm_new_password_label').html("");
                }else if(confirmPwd!=newPwd){
                    $('#confirm_new_password_label').html("<font color='red'>Invalid!</font>");
                }else{
                    $('#confirm_new_password_label').html("");
                }
                
                checkPwdStrength();
            }
            
            function checkPwdStrength(){
                var pwd = $('#new_password').val();
                
                var strongRegex = new RegExp( "^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g" );
                var mediumRegex = new RegExp( "^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g" );
                var enoughRegex = new RegExp( "(?=.{6,}).*", "g" );

                if (pwd.length == 0) {
                    var strVerdict = "Type Password";
                    $('#new_password_label').html("");
                } else if( false == enoughRegex.test( pwd ) ) {
                    var strVerdict = "Very Weak!";
                    $('#new_password_label').html("<font color=red>"+strVerdict+"</font>");
                } else if( strongRegex.test( pwd ) ) {
                    var strVerdict = "Strong!";
                    $('#new_password_label').html("<font color='#347235'>"+strVerdict+"</font>");
                } else if( mediumRegex.test( pwd ) ) {
                    var strVerdict = "Medium!";
                    $('#new_password_label').html("<font color='#41A317'>"+strVerdict+"</font>");
                } else {
                    var strVerdict = "Weak!";
                    $('#new_password_label').html("<font color=orange>"+strVerdict+"</font>");
                }
            }
            
            $(function(){
                $('#changePwd-form').form({
                    url:'../SystemUserServlet?action=changePwd',
                    onSubmit:function(){  
                        if($(this).form('validate')==true){
                            if($('#confirm_new_password_label').html()!=""){
                                alertMsgStr("Change Password","Please retype the correct new password","error");
                                return false;
                            }
                            var newPwdValidation = $('#new_password_label').html();
                            if(newPwdValidation.search("Weak")!=-1){
                                alertMsgStr("Change Password","Please ensure password is medium","error");
                                return false;
                            }
                            
                            return true;
                        }else{
                            return false;
                        }
                    },
                    success:function(data){
                        var obj = jQuery.parseJSON(data); 
                        if((obj.type!='error')){
                            alertMsgStr(obj.title,obj.content,obj.type);
                            $('#changePwd-dialog').dialog('close'); 
                        }else{
                            alertMsgStr(obj.title,obj.content,obj.type);
                        }
                    }
                });
            });
            
            
            function checkOldPassword(){
                var old_pwd = $('#old_password').val();
                $.ajax({
                    type: "POST",
                    url: "../SystemUserServlet",
                    data: "action=checkOldPwd&old_password="+old_pwd,
                    dataType: "json",
                    cache: false,
                    success: function(data){
                        if(data.type=="error"){
                            $('#old_password_label').html("<font color='red'>Invalid!</font>");
                        }else{
                            $('#old_password_label').html("");
                        }
                    }
                });
            
            }
            
            function processLogout(){
                window.parent.$.messager.confirm("Confirm", "Proceed with logout?", function(r){
                    if (r){
                        $.ajax({
                            type: "POST",
                            url: "../LoginServlet",
                            data: "action=logout",
                            dataType: "json",
                            cache: false,
                            success: function(data){
                                if((data.type!='error')){
                                    window.open('../admin.jsp',"_self");
                                }
                            }
                        });
                    }
                });
            }
        </script>
    </head>
    <body id="layout" name="layout" class="easyui-layout layoutBody"  style="text-align:left;">
        <div region="north" border="false" style="height:40px; width:100%; text-align:left; background:#efefef;">  
            <div style="position: absolute;  bottom: 0px; padding:1px; width:100%; border:0px solid #D2E0F2"> 
                &nbsp;&nbsp;
                <span style=" color: #000000; letter-spacing: -2px; font-family: Arial,Helvetica,sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 28px; line-height: 28px; font-size-adjust: none; font-stretch: normal">IEPM</span>
                <span style=" color: #FF6600; letter-spacing: -1px; font-family: Arial,Helvetica,sans-serif; font-style: normal; font-variant: normal; font-weight: bold; font-size: 28px; line-height: 28px; font-size-adjust: none; font-stretch: normal">admin</span>
                <span style="position: absolute;  right: 5px;">
                    <a href="#" class="easyui-menubutton" menu="#mm1" iconCls="icon-user"><label id="emp_name_menu" name="emp_name_menu"></label></a> 
                    <a  href="#" class="easyui-linkbutton" plain="true" onclick="processLogout()" iconCls="icon-logout">Logout</a>  
                </span>
            </div>
            <div id="mm1" style="width:150px;"> 
                <div iconCls="icon-layout"><a href="home.jsp" target="content">Home</a></div>
                <div iconCls="icon-gears"><a href="#" onclick="changePwd()">Change Password</a></div>  
            </div>  
        </div>
        <div region="west" border="true" title="&nbsp;Menu" style="width:200px; padding:5px;">
            <ul id="mntree" name="mntree" class="easyui-tree">
                <%if (am.get("G").equals("true")) {%>
                <li iconCls="icon-window">
                    <span>General Mgmt</span>  
                    <ul>  
                        <li iconCls="icon-box"><span><a href="configurationMgmt.jsp" target="content" >Configuration</a></span></li> 
                        <li iconCls="icon-box"><span><a href="systemUserMgmt.jsp" target="content" >System User</a></span></li> 
                    </ul>
                </li>
                <%}%>
                <%if (am.get("T").equals("true")) {%>
                <li iconCls="icon-window">
                    <span>Theme Mgmt</span>  
                    <ul>  
                        <li iconCls="icon-box"><span><a href="themeMgmt.jsp?industry=1" target="content" >Healthcare</a></span></li>  
                        <li iconCls="icon-box"><span><a href="themeMgmt.jsp?industry=2" target="content" >Hotel</a></span></li>  
                        <li iconCls="icon-box"><span><a href="themeMgmt.jsp?industry=3" target="content" >Logistics</a></span></li>  
                    </ul>
                </li>
                <%}%>
                <%if (am.get("Q").equals("true")) {%>
                <li iconCls="icon-window">
                    <span>Question Mgmt</span>  
                    <ul>  
                        <li iconCls="icon-box"><span><a href="questionMgmt.jsp?industry=1" target="content" >Healthcare</a></span></li>
                        <li iconCls="icon-box"><span><a href="questionMgmt.jsp?industry=2" target="content" >Hotel</a></span></li>
                        <li iconCls="icon-box"><span><a href="questionMgmt.jsp?industry=3" target="content" >Logistics</a></span></li>
                    </ul>  
                </li>
                <%}%>
                <%if (am.get("R").equals("true")) {%>
                <li iconCls="icon-window">
                    <span>Recommendation Mgmt</span>  
                    <ul>  
                        <li iconCls="icon-box"><span><a href="recommendationMgmt.jsp?industry=1" target="content" >Healthcare</a></span></li>
                        <li iconCls="icon-box"><span><a href="recommendationMgmt.jsp?industry=2" target="content" >Hotel</a></span></li>
                        <li iconCls="icon-box"><span><a href="recommendationMgmt.jsp?industry=3" target="content" >Logistics</a></span></li>
                    </ul>  
                </li>
                <%}%>
                <%if (am.get("A").equals("true")) {%>
                <li iconCls="icon-window">
                    <span>Attempt Mgmt</span>  
                    <ul>  
                        <li iconCls="icon-box"><span><a href="attemptMgmt.jsp?industry=1" target="content" >Healthcare</a></span></li>
                        <li iconCls="icon-box"><span><a href="attemptMgmt.jsp?industry=2" target="content" >Hotel</a></span></li>
                        <li iconCls="icon-box"><span><a href="attemptMgmt.jsp?industry=3" target="content" >Logistics</a></span></li>
                    </ul>  
                </li> 
                <%}%>
                <%if (am.get("P").equals("true")) {%>
                <li iconCls="icon-window">
                    <span>Reports</span>  
                    <ul>  
                        <li iconCls="icon-box"><span><a href="trend.jsp" target="content" >Trend</a></span></li>                          
                        <li iconCls="icon-box"><span><a href="statistics.jsp" target="content" >Statistics</a></span></li>
                        <li iconCls="icon-box"><span><a href="industryOpinion.jsp" target="content" >Industry's Opinion</a></span></li>  
                        <li iconCls="icon-box"><span><a href="firmPriority.jsp" target="content" >Firm's Priority</a></span></li>
                    </ul>
                </li>
                <%}%>
            </ul>
        </div>
        <div border="true" region="center" style="padding:0px; overflow:hidden; ">        
            <iframe id="content" name="content" scrolling="auto" frameborder="0"  src="home.jsp" style="width:100%;height:100%; background:#ffffff;"></iframe>
        </div>

        <div id="changePwd-dialog" title="Change Password" style="width:350px; height:215px">
            <form id="changePwd-form" method="post">
                <div id="changePwd-div">
                    <table id="changePwdTable">
                        <tr>
                            <td colspan="3"><font color="red">All fields are required</font><br/>
                                <font color="blue">Password requires minimum of 7 Alpha-Numeric Characters</font>
                                <br/><br/></td>
                        </tr>
                        <tr>
                            <td>
                                Old Password:
                            </td>
                            <td>
                                <input id="old_password" name="old_password" class="easyui-validatebox" onchange="checkOldPassword(this)" type="password" required="true"/>
                            </td>
                            <td>
                                <label id="old_password_label" name="old_password_label"></label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                New Password:
                            </td>
                            <td>
                                <input class="easyui-validatebox" id="new_password" name="new_password" type="password" onkeyup="checkConfirmPwd()" onkeypress="checkConfirmPwd()" onfocus="checkConfirmPwd()" onchange="checkConfirmPwd()" required="true"/><br/>
                            </td>
                            <td>
                                <label id="new_password_label" name="new_password_label"></label>
                            </td>
                        </tr>
                        <tr>
                        <tr>
                            <td>
                                Confirm New Password:
                            </td>
                            <td>
                                <input class="easyui-validatebox" id="confirm_new_password" name="confirm_new_password" type="password" onkeyup="checkConfirmPwd()" onkeypress="checkConfirmPwd()" onfocus="checkConfirmPwd()" onchange="checkConfirmPwd()" required="true"/>
                            </td>
                            <td>
                                <label id="confirm_new_password_label" name="confirm_new_password_label"></label>
                            </td>
                        </tr>
                    </table>
                </div>
                <br/>
                <div class="form_buttons" id="form_buttons">
                    <input type="submit" onclick="" value="OK"/>
                    <input type="button" onclick="onClickCancel()" value="Cancel" />
                </div>
            </form>
        </div>
        <div id="resetPwd-dialog" title="Loading..." >
            <p></p>
            <table>
                <tr>
                    <td align="top">
                        <img src="./css/images/ajax-loader.gif"/>
                    </td>
                    <td>
                        <br/>
                    </td>
                    <td>
                        <br/>
                    </td>
                    <td>
                        <p>
                            Loading... Please Wait!<br/>
                            System is sending a notification email to the user...
                        </p>
                    </td>
                </tr>
            </table>
            <p></p>
        </div>
    </body>
</html>
